
import { _decorator, Component, Node, Sprite, UITransform,Animation, AnimationClip, animation, SpriteFrame } from 'cc';
import { randomByLength } from '../../Utils';
import { ENTITY_TYPE_ENUM, PARAMS_NAME_ENUM, DIRECTION_ORDER_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM, EVENT_ENUM, ENTITY_STATE_ENUM } from '../../Enum';
import { IEntity, ISpikes } from '../../Levels';
import { PlayerStateMachine } from '../Player/PlayerStateMachine';
import { TILE_WIDTH, TILE_HEIGHT } from '../Tile/TileManager';
import { SpikesStateMachine } from './SpikesStateMachine';
import EventManager from '../../Runtime/EventManager';
import DataManager from '../../Runtime/DataManager';
// import { TILE_HEIGHT, TILE_WIDTH } from '../Tile/TileManager';
// import ResourceManager from '../../Runtime/ResourceManager';
// import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, EVENT_ENUM,   PARAMS_NAME_ENUM } from '../../Enum';
// import EventManager from '../../Runtime/EventManager';
// import { PlayerStateMachine } from './PlayerStateMachine';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = PlayerManager
 * DateTime = Tue Jun 27 2023 13:04:45 GMT+0800 (中国标准时间)
 * Author = zshzshzsh15
 * FileBasename = PlayerManager.ts
 * FileBasenameNoExtension = PlayerManager
 * URL = db://assets/Script/Player/PlayerManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */


const ABIMATION_SPEED=1/8  //帧数  8帧
@ccclass('SpikesManager')
export class SpikesManager extends Component {
    id:string =randomByLength(12)
    x:number
    y:number 
    fsm:PlayerStateMachine

    
    private _count:number
    //人物当前的方向
    private  _totalCount:number

    type:ENTITY_TYPE_ENUM

    //数据ui分离
    get count(){
        return this._count
    }


    set count(newCount:number){
        this._count=newCount
        this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_CUR_COUNT,newCount)
        //console.log(DIRECTION_ORDER_ENUM[this._driection])是个数字
    }

    get totalCount(){
        return this._totalCount
    }


    set totalCount(newCount){
        this._totalCount=newCount
        this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT,newCount)
    }


    update(){
        
        this.node.setPosition(this.x*TILE_WIDTH-TILE_WIDTH*1.5,-this.y*TILE_HEIGHT+TILE_HEIGHT*1.5)
    }


    //外面传参进来改变
    async  init(params:ISpikes){
        //拆的代码
        const sprite = this.addComponent(Sprite)
        sprite.sizeMode=Sprite.SizeMode.CUSTOM  //能自定义大小



        const ui=this.getComponent(UITransform)
        ui.setContentSize(TILE_WIDTH*4,TILE_HEIGHT*4)

        
        
        //控制turnleft组件
        this.fsm=this.node.addComponent(SpikesStateMachine)
        await this.fsm.init() //初始化状态机


        this.x=params.x
        this.y=params.y

        this.type=params.type
        // //设置初始方向
        // this.direction=DIRECTION_ENUM.TOP

        this.totalCount=SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[this.type ]

        // //设置当前人物状态
        // this.state=ENTITY_STATE_ENUM.IDLE
        this.count=params.count

        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END,this.onLoop,this)
    }

    onDestroy(){
        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END,this.onLoop)

    }

    onLoop(){
        if(this.count===this.totalCount){
            this.count=1
        }else{
            this.count++

        }

        this.onAttack()
    }

    onAttack(){
        if(!DataManager.Instance.player){
            return  
        }


        const {x:playx,y:playy}=DataManager.Instance.player
        if(this.x===playx && this.y===playy && this.count===this.totalCount){
            console.log("onAttack")
            EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER,ENTITY_STATE_ENUM.DEATH)
        }
    }


    backZero(){
        this.count=0
    }



    //让xy趋近目标



    //实现人物的移动,参数为方向的类型



    //渲染
    // async render(){
    //     const sprite = this.addComponent(Sprite)
    //     sprite.sizeMode=Sprite.SizeMode.CUSTOM  //能自定义大小

    //     const ui=this.getComponent(UITransform)
    //     ui.setContentSize(TILE_WIDTH*4,TILE_HEIGHT*4)


    //     //加载资源
    //     const spriteFrames=await ResourceManager.Instance.loadDir("texture/player/idle/top")

    //     const animationsComponent=this.addComponent(Animation)

    //     //甚至clip
    //     const animationClip = new AnimationClip()
        

    //     //轨道
    //     const track =new animation.ObjectTrack()//创建对象轨道
    //     track.path = new animation.TrackPath().toComponent(Sprite).toProperty('spriteFrame'); // 指定轨道路径，即指定目标对象为 "Foo" 子节点的 "position" 属性


    //     //处理资源数组
    //     const frams :   Array<[number,SpriteFrame]>=spriteFrames.map((item,index)=>[ABIMATION_SPEED*index,item]) //得到关键帧列表
    //     track.channel.curve.assignSorted(
    //         frams
    // //         [ // 为 x 通道的曲线添加关键帧
    // //     [0.4, ({ value: 0.4 })],
    // //     [0.6, ({ value: 0.6 })],
    // //     [0.8, ({ value: 0.8 })],
    // // ]
    // )
        
    //     //最后将轨道添加到动画剪辑
    //     animationClip.addTrack(track)

    //     animationClip.wrapMode=AnimationClip.WrapMode.Loop
        
    //     //
    //     animationClip.duration=frams.length*ABIMATION_SPEED //整个动画剪辑的周期
    //     animationsComponent.defaultClip=animationClip
    //     animationsComponent.play()
    // }



    
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
